package com.bangbang.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.mapper.TbCatalogMapper;
import com.bangbang.mapper.TbCourseMapper;
import com.bangbang.mapper.TbFileMapper;
import com.bangbang.mapper.TbSectionMapper;
import com.bangbang.pojo.TbCatalog;
import com.bangbang.pojo.TbFile;
import com.bangbang.pojo.TbSection;
import com.bangbang.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 课程商品表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-11
 */
@RequestMapping("/tbCourse")
@RestController
public class TbCourseController {


    @Autowired
    private TbCourseMapper tbCourseMapper;

    @Autowired
    private TbCatalogMapper tbCatalogMapper;

    @Autowired
    private TbFileMapper tbFileMapper;

    @Autowired
    private TbSectionMapper tbSectionMapper;

    //  查
    @ApiOperation(value = "课程目录的查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couId",value = "课程详情的id",required = true,paramType = "path"),
    })
    @GetMapping("/{couId}")
    public Result selectList(@PathVariable("couId") Integer couId){
        if (couId == null){
            return Result.buildResult(ResultEnum.ERROR);
        }else {
            Map<String, Object> map = new HashMap<>();
            QueryWrapper<TbCatalog> catalogQueryWrapper = new QueryWrapper<>();
            catalogQueryWrapper.eq("ca_course_id", couId);
//        目录名和id
            TbCatalog tbCatalog = tbCatalogMapper.selectOne(catalogQueryWrapper);
            if (tbCatalog == null){
                return Result.buildResult(ResultEnum.ERROR);
            }
            String caName = tbCatalog.getCaName();
            map.put("catalogName", caName);
            Long caId = tbCatalog.getCaId();
//        查找章节
            QueryWrapper<TbSection> sectionQueryWrapper = new QueryWrapper<>();
            sectionQueryWrapper.eq("se_catalog_id", caId);
//        查找名字和id;
            List<TbSection> tbSections = tbSectionMapper.selectList(sectionQueryWrapper);
            List<Map<String, Object>> lsit = new ArrayList<>();
            for (TbSection tbSection : tbSections) {
                Long seId = tbSection.getSeId();
                String seName = tbSection.getSeName();
                QueryWrapper<TbFile> fileQueryWrapper = new QueryWrapper<>();
                Map<String, Object> map1 = new HashMap<>();
//            查文件
                fileQueryWrapper.eq("fl_section_id", seId);
                List<TbFile> tbFiles = tbFileMapper.selectList(fileQueryWrapper);
                List<Map<String, Object>> list1 = new ArrayList<>();
                for (TbFile tbFile : tbFiles) {
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("file", tbFile);
                    list1.add(map2);
                    map1.put("SectionName", tbSection.getSeName());
                }
                map1.put("list", list1);
                lsit.add(map1);
            }
            map.put("Zhanglist", lsit);
            String catalogList = JSONObject.toJSONString(map);
            System.out.println(catalogList);
            return Result.buildResult(ResultEnum.SUCCESS).setResult(catalogList);
        }
    }
//       ---------------------------------增加-------------------------------------------------

    @ApiOperation(value = "目录增加方法") //  接口文档方法名
    @ApiImplicitParams({            //  接口文档  解释参数
            @ApiImplicitParam(name = "couId",value = "课程id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "catalogList",value = "Json格式字符串",required = true,paramType = "query"),
    })
    @PostMapping         //  post 请求 新增 复杂查询等方法
    public Result save(@RequestParam Long couId,@RequestParam String catalogList){
        Map maps = (Map) JSON.parse(catalogList);
        Object catalogName = maps.get("catalogName");
        //            增加目录表
            TbCatalog tbCatalog = new TbCatalog();
            IdWorker idWorker = new IdWorker();
            long l = idWorker.nextId();
            tbCatalog.setCaId(l).setCaWhetherDelete(0).setCaName(catalogName.toString()).setCaCourseId(couId);
            tbCatalogMapper.insertCatelog(tbCatalog);
        Object zhanglist = maps.get("Zhanglist");
        List parse2 = (List) JSON.parse(JSONObject.toJSONString(zhanglist));
        for (Object o : parse2) {
            String string = o.toString();
            Map ma = (Map) JSON.parse(string);
            Object sectionName = ma.get("SectionName");
            Object list = ma.get("list");
//        增加章节表
            TbSection tbSection = new TbSection();
            IdWorker id  = new IdWorker();
            long l1 = id.nextId();
            String string3 = sectionName.toString();
            tbSection.setSeId(l1).setSeName(string3).setSeCatalogId(l);
            tbSectionMapper.insertSection(tbSection);
            List parse = (List) JSON.parse(JSONObject.toJSONString(list));
            for (Object o1 : parse) {
                String string1 = o1.toString();
                Map m = (Map) JSON.parse(string1);
                Object file = m.get("file");
                System.out.println("文件   "+file);
                String string2 = file.toString();
                Map m1 = (Map) JSON.parse(string2);
                Object flName = m1.get("flName");
                String string4 = flName.toString();
                Object flUrl = m1.get("flUrl");
                String string5 = flUrl.toString();
//                增加文件表
                IdWorker i  = new IdWorker();
                long l2 = i.nextId();
                TbFile tbFile = new TbFile();
                tbFile.setFlUrl(string5).setFlSectionId(l1).setFlName(string4).setFiId(l2);
                tbFileMapper.insertFile(tbFile);
                System.out.println("文件名字    "+flName);
                System.out.println("路径     "+flUrl);
            }
            System.out.println(ma);
            System.out.println("文件集合  "+list);
            System.out.println("章节名    "+sectionName);
        }
        return Result.buildResult(ResultEnum.SUCCESS).setResult(l);
    }
//     - - - - - - - - - - - 删除 - - - - - - - - - - - - - - - - -

    //  删
    @ApiOperation(value = "目录删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId",value = "目录的id",required = true,paramType = "path"),
    })
    @DeleteMapping("/{courseId}")
    public Result delete(@PathVariable("courseId") Long courseId){
//        删除目录
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<TbCatalog> catalogQueryWrapper = new QueryWrapper<>();
        catalogQueryWrapper.eq("ca_course_id", courseId);
        TbCatalog tbCatalog = tbCatalogMapper.selectOne(catalogQueryWrapper);
        Long caId = tbCatalog.getCaId();
//        删除章节
            //1. 查询父级id有seId的章节
        Map<String, Object> map2= new HashMap<>();
        QueryWrapper<TbSection> sectionQueryWrapper1 = new QueryWrapper<>();
        sectionQueryWrapper1.eq("se_catalog_id", caId);
        List<TbSection> tbSections = tbSectionMapper.selectList(sectionQueryWrapper1);
        for (TbSection tbSection : tbSections) {
            Long seId = tbSection.getSeId();
            Map<String, Object> map3 = new HashMap<>();
            QueryWrapper<TbFile> fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.eq("fl_section_id", seId);
            tbFileMapper.delete(fileQueryWrapper);
        }
        tbSectionMapper.delete(sectionQueryWrapper1);
        tbCatalogMapper.delete(catalogQueryWrapper);
        return Result.buildResult(ResultEnum.SUCCESS);
    }










//    public static void main(String[] args) {
//       String  str = "{\"catalogName\":\"java学习教程大全\",\"Zhanglist\":[{\"SectionName\":\"第一天\",\"list\":[{\"file\":{\"fiId\":1,\"flName\":\"java的发展\",\"flSectionId\":1,\"flUrl\":\"djiwadi.djiwijd\"}},{\"file\":{\"fiId\":2,\"flName\":\"jiava多线程\",\"flSectionId\":1,\"flUrl\":\"duawuw.dwa\"}}]}]}\n";
//        Map maps = (Map) JSON.parse(str);
//        Object catalogName = maps.get("catalogName");
//        Object zhanglist = maps.get("Zhanglist");
//        List parse2 = (List) JSON.parse(JSONObject.toJSONString(zhanglist));
//        for (Object o : parse2) {
//            String string = o.toString();
//            Map ma = (Map) JSON.parse(string);
//            Object sectionName = ma.get("SectionName");
//            Object list = ma.get("list");
//            List parse = (List) JSON.parse(JSONObject.toJSONString(list));
//            for (Object o1 : parse) {
//                String string1 = o1.toString();
//                Map m = (Map) JSON.parse(string1);
//                Object file = m.get("file");
//                System.out.println("文件   "+file);
//                String string2 = file.toString();
//                Map m1 = (Map) JSON.parse(string2);
//                Object flName = m1.get("flName");
//                Object flUrl = m1.get("flUrl");
//                System.out.println("文件名字    "+flName);
//                System.out.println("路径     "+flUrl);
//            }
//            System.out.println(ma);
//            System.out.println("文件集合  "+list);
//            System.out.println("章节名    "+sectionName);
//            System.out.println("目录名字    "+catalogName);
//        }
//        /*String string = zhanglist.toString();
//        Object parse = JSONArray.parse(string);
//        Map map = (Map) JSON.parse(str);
//        String string1 = parse.toString();
//        Object parse1 = JSONArray.parse(string1);
//        System.out.println(parse1);
//
//        System.out.println("这个是用JSON类来解析JSON字符串!!!");*/
//
//
//
////        System.out.println(map);
////        for (Object map : maps.entrySet()) {
////            Object value = ((Map.Entry) map).getValue();
////            String substring = value.toString();
////            System.out.println(substring);
//////            String name = substring[0];
//////            System.out.println(((Map.Entry) map).getKey() + "     " + ((Map.Entry) map).getValue());
////        }
//    }
}

